package org.ii.dataprovider.dapper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;

import org.ii.dataprovider.dapper.Dapp;


public class RunDapps {
	
	public static void main(String[] args) {
		
		Dapp dapp;
		
		try {
      
      // Example 1
			//----------
			
			System.out.println("Performing MSN Search for 'good times' using variable arguments\n");
			dapp = new Dapp("MSNSearchResults",new String[]{"good times"});
			// Print the URLs of all the search results using the DOM
			Iterator searchResultsItr = dapp.getDOM().getRootElement().elementIterator("Search_Result");
			while(searchResultsItr.hasNext()){
				Element searchResults = (Element) searchResultsItr.next();
				Iterator titlesItr = searchResults.elementIterator("Title");
				while(titlesItr.hasNext()){
					Element title = (Element) titlesItr.next();
					System.out.println("Search Result URL: " + title.attributeValue("href"));
				}
			}
			System.out.println("\n");
			
			
			
			// Example 2
			//----------
			
			System.out.println("Performing MSN Search for 'good times' using applyToUrl\n");
			dapp = new Dapp("MSNSearchResults","http://search.msn.com/results.aspx?q=good%20times");
			// Print the number of search results using the DOM
			Document domDoc = dapp.getDOM();
			System.out.println("Number of results: " + domDoc.getRootElement().elementText("Number_of_Results") + "\n");
			System.out.println("\n");
			
			
			// Example 3
			//----------
			
			System.out.println("Determining size of returned XML for Fandango Theaters\n");
			dapp = new Dapp("FandangoTheaters");
			// Print the size in bytes of the XML
			System.out.println("Size in bytes of XML: " + dapp.getXML().length() + "\n");
			System.out.println("\n");
			
			
			// Example 4
			//----------
			
			// Translate "Dapper is cool" to French
			System.out.println("Translating 'Dapper is very cool' to French using Babelfish\n");
			Map<String,String> variableArgs = new HashMap<String,String>();
			variableArgs.put("trtext","Dapper is very cool");
			variableArgs.put("lp","en_fr");
			dapp = new Dapp("Babelfish", variableArgs);
			System.out.println(dapp.getDOM().getRootElement().elementText("Translated_Text"));
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}
